package com.itc.dispatch.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itc.common.core.utils.OkHttpUtil;
import com.itc.common.core.utils.StringUtils;
import com.itc.dispatch.domain.FlightOperationMonitoring;
import com.itc.dispatch.domain.FlightOperationMonitoringPtd;
import com.itc.dispatch.service.FlightOperationMonitoringPtdService;
import com.itc.dispatch.mapper.FlightOperationMonitoringPtdMapper;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 91516
* @description 针对表【flight_operation_monitoring_ptd(航班预测起飞算法数据信息表)】的数据库操作Service实现
* @createDate 2024-08-13 09:37:04
*/
@Service
public class FlightOperationMonitoringPtdServiceImpl extends ServiceImpl<FlightOperationMonitoringPtdMapper, FlightOperationMonitoringPtd>
    implements FlightOperationMonitoringPtdService{

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void ptdDataUpdate() {
        //api获取宽表数据
        Object resultObj = OkHttpUtil.sendOkHttpClient("http://172.23.80.2:8080/alg/ptd/getPtd"
                , "GET",null,null);
        if(resultObj == null){
            return;
        }
        String dataString = resultObj.toString();
        JSONObject dataJson = JSONObject.parseObject(dataString);
        if(!StringUtils.equals("200",dataJson.getString("code"))){
            System.out.println(dataJson);
            return;
        }

        JSONArray monitoringList = JSONArray.parseArray(dataJson.getString("data"));
        if(monitoringList.size() > 0){
            try{
                List<FlightOperationMonitoringPtd> ptdList = new LinkedList<>();
                for (Object o : monitoringList) {
                    FlightOperationMonitoringPtd ptd = JSONObject.parseObject(o.toString(),FlightOperationMonitoringPtd.class);
                    ptdList.add(ptd);
                }

                if(ptdList.size() > 0){
                    List<String> flightIds = ptdList.stream().map(FlightOperationMonitoringPtd::getFlightIdD).collect(Collectors.toList());
                    this.getBaseMapper().delete(new LambdaQueryWrapper<FlightOperationMonitoringPtd>().in(FlightOperationMonitoringPtd::getFlightIdD,flightIds));
                    this.saveBatch(ptdList);
                }

            }catch (Exception e){
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        String s = "[{\"boundary\":\"IDUMA\",\"dstd\":\"2024-08-13 11:10:00\",\"flight_id_d\":\"26974510\",\"normal_release_time\":\"2024-08-13 14:10:00\",\"normal_takeoff_time\":\"2024-08-13 10:35:00\",\"ptd\":\"2024-08-13 11:00:00\",\"release_delay_min\":0.0,\"release_delay_status\":\"\\u9884\\u8ba1\\u653e\\u884c\\u6b63\\u5e38\",\"runway\":\"15\",\"take_ranks\":15,\"takeoff_delay_min\":25.0,\"takeoff_delay_status\":\"\\u9884\\u8ba1\\u8d77\\u98de\\u4e0d\\u6b63\\u5e38\"}]";

        JSONArray monitoringList = JSONArray.parseArray(s);
        for (Object o : monitoringList) {
            FlightOperationMonitoringPtd ptd = JSONObject.parseObject(o.toString(),FlightOperationMonitoringPtd.class);
            System.out.println(ptd.getPtd());
        }
        System.out.println(monitoringList);

    }
}




